﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using PBMessage;
using SGLisp;
using GameUnit;
using Pathfinding;
public class GameScene : BaseScene
{
    public GameSceneTrigger SceneTrigger = new GameSceneTrigger();

    public GamePlayer LocalPlayer;
    public Dictionary<long, GamePlayer> Players = new Dictionary<long, GamePlayer>();

    public GameObject SceneG;
    public GameObject UnitG;

    GameMap GameMap = new GameMap();
    public GameInputManager InputManager = new GameInputManager();
    private long _ServerTime;
    public long ServerTime { get { return _ServerTime; } }

    AsyncOperation ayscLoadSceneState;
    bool IsSceneLoad = false;
    Hashtable mapDic;
    List<ReadRoomPlayer> readyPlayers;

    public ThirdCamera LocalCamera;
    public override void OnLoad(params object[] args)
    {
        mapDic = (Hashtable)args[0]; 
        readyPlayers = (List<ReadRoomPlayer>)args[1];
        ayscLoadSceneState =  Application.LoadLevelAsync((string)mapDic[":map-name"]);
    }

    public void OnSceneLoad()
    {
        //初始化UI
        ExcelTableRender.SetTablePath("Table/Excel/");
        Game.UI.Init(ExcelTableRender.GetTable("UIWidgets"), ExcelTableRender.GetTable("UILayers"));
       
        SceneG = new GameObject("SceneObject");
        UnitG = new GameObject("UnitObject");
        UnitG.transform.parent = SceneG.transform;
        //GameMap.Load(mapDic);
       
        //初始化玩家信息
        foreach (ReadRoomPlayer readyPlayer in readyPlayers)
        {
            GamePlayer newPlayer = new GamePlayer(readyPlayer);
            this.Players[newPlayer.UserId] = newPlayer;
            if (newPlayer.Account == Game.Model.Get<PlayerModel>().Account)
            {
                this.LocalPlayer = newPlayer;
            }
        }
        //初始化触发器
        this.SceneTrigger.Trigger.Init();
        SceneTrigger.DefineActions();
        //发送进入场景成功消息
        CS_EnterGameSceneOK enterSceneOK = new CS_EnterGameSceneOK();
        enterSceneOK.UserId = LocalPlayer.UserId;
        Game.Net.SendPacket(enterSceneOK, PBMessageID.CS_EnterGameSceneOK);
        //摄像机
        GameObject thirdCameraG = new GameObject("LocalCamera");
        this.LocalCamera = thirdCameraG.AddComponent<ThirdCamera>();
        object[] OffsetPosArr = (object[])mapDic[":camera-offset"];
        this.LocalCamera.Init(new Vector3((float)OffsetPosArr[0], (float)OffsetPosArr[1], (float)OffsetPosArr[2]));
        Game.Event.AddHandler("GameLogicLoad", GameLogicLoad);


        WayPointRoot wayPointRoot = GameObject.FindObjectOfType<WayPointRoot>();
        wayPointRoot.LoadFromConfig((Hashtable)SGLisp.Helper.EvalValueFromFile("D:/UnityPro/StarParty/Public/GameTable/Map/waypoints.txt"));
    }

    public void GameLogicLoad(object p1,object p2)
    {
        Game.UI.Show("SkillBar");
    }

    public void CreateUnit(string unitName, long uuid, long playerId)
    {
        this.Players[playerId].MainUnit = UnitManager.CreateUnit(unitName, uuid, UnitG.transform);
        this.Players[playerId].MainUnit.GamePlayer = this.Players[playerId];
        this.Players[playerId].MainUnit.OnLoad();
    }

    float addTick;
    public void ServerUpdate(SC_ServerUpdate updatePacket)
    {
        this._ServerTime = updatePacket.ServerTime;
        addTick += Time.deltaTime;
        foreach (KeyValuePair<long, Unit> unit in UnitManager.Units)
        {
            unit.Value.ServerUpdate(updatePacket);
        }
        if (addTick > 2f)
        {
            addTick = 0;
            CS_GameSceneTick tick = new CS_GameSceneTick();
            tick.ClientTime = System.DateTime.Now.Ticks;
            Game.Net.SendPacket<CS_GameSceneTick>(tick, PBMessageID.CS_GameSceneTick);
        }
    }

    public override void OnUpdate()
    {
        if (this.ayscLoadSceneState.isDone&&this.IsSceneLoad==false)
        {
            this.IsSceneLoad = true;
            this.OnSceneLoad();
        }
        if (this.IsSceneLoad)
        {
            SceneTrigger.Trigger.Update();
            this.InputManager.Update();
        }
    }
}
